from PyQt4.QtGui import QGraphicsObject,QPen,QBrush,QColor,QGraphicsTextItem,QFont
from PyQt4 import QtCore,QtGui,Qt
from PyQt4.QtCore import QPointF,QSizeF,QObject,QRectF,QRect
from KCharts.KGraphics.KGraphicsSceneSubViewModel import KGraphicsSceneSubViewModel
from KCharts.KGraphics.KBaseDisplayItem import KBaseDisplayItem,KBaseDisplayItemModel
from KApp import KConstants as vm
import time

class KMouseItemModel(KBaseDisplayItemModel):

	def GetLogicRange(self):
		(xdatarange,ydatarange)=self.m_stock.CurWindowRange('Price')
		return (xdatarange,ydatarange)
	
	
class KMouseDisplayItem(KBaseDisplayItem):
	# handle the mouse cross hair and dynamic text drawing, assuming the price view and vol view exist.
	def __init__(self,viewmodel,stockobj):
		super(KMouseDisplayItem,self).__init__(viewmodel,stockobj)
		self.GridSpace=[50,80] # the space between each grid line
		self.IsOutOfBound=False # track the last position out of drawing bound or not
		self.setZValue(10) #move to the top of other items
		
		self.legendtextitem=QGraphicsTextItem(self) #maintain the top-lef corner legend
		self.legendtextitem.setZValue(9)

		
	def OnMouseMove(self,pos):
		

		mrect=self.viewmodel.GetViewScreenRect(vm.RectType.ContentBound)
		if (not mrect.contains(pos)) and self.IsOutOfBound==True: 
			return 
		if mrect.contains(pos):
			self.IsOutOfBound=False
			self.curpos=pos	
		else:
			self.IsOutOfBound=True
			self.curpos=QPointF()
		self.update()	

	def GetLabel(self,symbolname,time,priceblock):
		result='Symbol: % s Date: %s Open: %.2f High: %.2f Low: %.2f Close: %.2f' %(symbolname.upper(),time,priceblock[0],priceblock[1],priceblock[2],priceblock[3])
		result="<span style='font-size: 10pt; font-family: times, serif; color: white;'>%s</span>" %(result)
		return result
	
	def PaintMouseCross(self,painter,RectType,pos):
		if pos.isNull()==True:
			return
		painter.setPen(self.GetPen('mousePen'))	
		painter.setFont(self.GetFont('labelText'))
		
		pricemodel=self.scene().GetViewByName('PriceView')
	
		[Logicx],[logicy]=pricemodel.mapScreenCordToLogic(RectType,[pos.x()],[pos.y()])
		
		lefrect=self.viewmodel.GetViewRect(vm.RectType.YAxisLeft)
		toprect=self.viewmodel.GetViewRect(vm.RectType.XAxisTop)
		mrect=self.viewmodel.GetViewScreenRect(vm.RectType.ContentBound)	
		inrect=self.viewmodel.GetViewScreenRect(vm.RectType.InnerBound)	
		Logicx=int(round(Logicx)) # the index of stock data (also the "logic X" coordinates of stock)
		Times=self.m_stock.CurWindowData('Time')
		prices=self.m_stock.CurWindowData('Price')
		symbolname=self.m_stock.GetSymbolName()
		XCordinates=self.m_stock.CurWindowData('Index')
		if Logicx in XCordinates:
			xindex=XCordinates.index(Logicx)
			stime=str(Times[xindex])
			priceblock=prices[xindex]
			self.curpos=pos

			painter.drawLine(QtCore.QPointF(pos.x(), mrect.top()), QtCore.QPointF(pos.x(), mrect.bottom()))
			painter.drawLine(QtCore.QPointF(mrect.left(), pos.y()), QtCore.QPointF(mrect.right(), pos.y()))	
			painter.drawText(QtCore.QPointF(lefrect.left()+10, pos.y()),"%.2f" %(logicy)) # print dynamic price
			painter.drawText(QtCore.QPointF(pos.x(), toprect.bottom()-10),stime) # print dynamic date
			self.legendtextitem.setPos(inrect.left(),inrect.top())
			self.legendtextitem.setHtml(self.GetLabel(symbolname,stime,priceblock)) # update the legend information
	
	def PrepareRenderPicture(self):
		
		self.picture = QtGui.QPicture()
		painter = QtGui.QPainter(self.picture)	
		self.PaintBorders(painter,vm.RectType.OuterBound)
		self.PaintBorders(painter,vm.RectType.InnerBound)
		self.PaintBorders(painter,vm.RectType.ContentBound)
		if hasattr(self,'curpos'):
			self.PaintMouseCross(painter,vm.RectType.ContentBound,self.curpos)
		
		
		painter.end()	
		

		
